﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
       
        private void MainForm_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;

            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
            BackColor = Color.Orange;
            this.CenterToScreen();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        //删除
        private void button4_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];
                var id = (int)row.Cells["id"].Value;
                var res = MessageBox.Show("确定删除？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var rowCount = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (rowCount > 0)
                    {
                        var name = textBox1.Text;
                        var dt = string.Format("select * from Articles where Author like '%{0}%'", name);
                        var table = DbHelper.GetDataTable(dt);
                        dataGridView1.DataSource = table;
                        MessageBox.Show("删除成功", "提示");
                    }
                }
            }
            else
            {
                MessageBox.Show("没有选择任何的行", "提示");
            }
        }
        //查找
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;
            var book = textBox1.Text;
            var sql = string.Format("select * from Articles where Author like'%{0}%'or Title like'%{1}%'", name,book);
            var dt = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt;
        }
        //增加
        private void button2_Click(object sender, EventArgs e)
        {
            EditForm edit = new EditForm();
            var res = edit.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;
                var dt = string.Format("select * from Articles where Author like'%{0}%'", name);
                var table = DbHelper.GetDataTable(dt);
                dataGridView1.DataSource = table;
            }
        }
        //修改
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var book = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var createdTime = (DateTime)dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                EditForm edit = new EditForm(id,book,name,createdTime);
                var res = edit.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var name1 = textBox1.Text;
                    var dt = string.Format("select * from Articles where Author like'%{0}%'", name1);
                    var table = DbHelper.GetDataTable(dt);
                    dataGridView1.DataSource = table;
                }
            }
            else
            {
                MessageBox.Show("你没有选择任何的数据行数", "提示");
            }
        }
    }
}
